import { ref } from 'vue';

function useRequestWithRetry<T>(
  request: () => Promise<T>,
  {
    retryCount = 3,
    retryDelay = 1000
  }
) {
  const loading = ref(false);
  const retry = async () => {
    let count = 1;
    
    while (count <= retryCount) {
      loading.value = true;
      console.log('retrying...', count);
      try {
        return await request();
      } catch (error) {
        count++;
        await new Promise((resolve) => setTimeout(resolve, retryDelay));
      } finally {
        loading.value = false;
      }
    }
  };

  return {
    request: retry,
    loading,
    // cancel,
  };
}

export { useRequestWithRetry };